package com.doit.mr.day06;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.Map;

/**
 * @DATE 2021/12/7/17:57
 * @Author MDK
 * @Version 2021.2.2
 **/
public class CombinerTest {
    static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //先处理行内容数据
            String line = value.toString();
            String[] words = line.split(" +");
            //将单词和次数输出
            for (String word : words) {
                Text k = new Text();
                IntWritable v = new IntWritable(1);
                context.write(k, v);
            }
        }
    }

    public static void main(String[] args) throws Exception{
        //统计单词出现的次数
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "wordcount" );
        //设置Mapper和Reducer类
        job.setMapperClass(WordCountMapper.class);

        //设置reduce输出的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //设置预聚合的类
        job.setCombinerClass(MyCombiner.class);
        FileInputFormat.setInputPaths(job, new Path("d://aaa//1.txt"));
        FileOutputFormat.setOutputPath(job, new Path("d://aaa//combiner"));
        job.waitForCompletion(true);
    }

}
